// 一个枚举对应一个状态
export class MutualExclusionSwitch<IDType, MemberType>{
    private switchMap: Map<IDType, MemberType[]>

    constructor(private key: keyof MemberType, private open: any, private close: any) {
        this.switchMap = new Map()
    }

    bind(id: IDType, ...target: MemberType[]) {
        if (!this.switchMap.has(id)) {
            this.switchMap.set(id, [])
        }
        let array = this.switchMap.get(id)
        for (let i = 0; i < target.length; i++) {
            if (!array.includes(target[i])) {
                array.push(target[i])
                continue
            }
            console.error('互斥状态选择器已包含此元素!!');
        }
    }

    select(selectedID: IDType) {
        for (const [id, targetArray] of this.switchMap) {
            for (const target of targetArray) {
                target[this.key] = id == selectedID ? this.open : this.close
            }
        }
    }
}